package com.tcs.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tcs.model.Article;
import com.tcs.model.Brand;
import com.tcs.service.BrandService;

@Controller
@Scope
public class BrandController {
	
	@Resource
	private BrandService brandService;
	
	
	@RequestMapping(value = "/saveBrand", method = RequestMethod.POST)
	public ModelAndView saveBrand(@ModelAttribute(" brand") Brand  brand,
			BindingResult result) {
		brandService.addBrand( brand);
		return new ModelAndView("redirect:/listBrand.html?id=1");
	}
	
	@RequestMapping(value = "/deleteBrand", method = RequestMethod.GET)
	public ModelAndView deleteBrand(
			@ModelAttribute(" brand") Brand brand, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int i = Integer.valueOf(request.getParameter("id"));
		brandService.deleteBrand(i);
		return new ModelAndView("redirect:/listBrand.html?id=1");
	}
	
	@RequestMapping(value = "/editBrand", method = RequestMethod.GET)
	public ModelAndView editBrand(
			@ModelAttribute(" brand") Brand brand, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int i = Integer.valueOf(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("brand", brandService.getBrand(i));
		return new ModelAndView("editBrand", model);
	}
	
	@RequestMapping(value = "/updateBrand", method = RequestMethod.POST)
	public ModelAndView updateBrand(
			@ModelAttribute(" brand") Brand brand, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int i = Integer.valueOf(request.getParameter("brandId"));
		brand.setBrandId(i);
		brandService.updateBrand(brand);
		return new ModelAndView("redirect:/listBrand.html?id=1");
	}
	
	@RequestMapping(value = "/listBrand", method = RequestMethod.GET)
	public ModelAndView listArticles(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("brands", brandService.listBrands());
		return new ModelAndView("brandsList", model);
		

	}

	@RequestMapping(value = "/addBrand", method = RequestMethod.GET)
	public ModelAndView addBrand(@ModelAttribute("brand") Brand brand,
			BindingResult result) {
		return new ModelAndView("addBrand");
	}

}
